package org.bigjava.travel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.bigjava.travel.bean.Strategy;
import org.bigjava.travel.dao.StrategyDao;
import org.bigjava.travel.service.StrategyService;
import org.bigjava.travel.utils.Tools;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 攻略的业务逻辑层实现类
 */
@Service
@Component
public class StrategyServiceImpl implements StrategyService {

    @Resource
    StrategyDao strategyDao;

    @Override
    public long count() throws Exception {
        return strategyDao.count();
    }

    @Override
    public Strategy findById(Integer id) throws Exception {
        return strategyDao.findById(id);
    }

    @Override
    public List<Strategy> findList() throws Exception {
        return strategyDao.indexList();
    }

    @Override
    public void save(Strategy strategy) throws Exception {
        strategyDao.save(strategy);
    }

    @Override
    public void user_save(Strategy strategy) throws Exception {
        strategyDao.user_save(strategy);
    }

    @Override
    public void update(Strategy strategy) throws Exception {
        strategyDao.update(strategy);
    }

    @Override
    public void deleteById(Integer id) throws Exception {
        strategyDao.deleteById(id);
    }

    public List<Strategy> findByPage(int currentPage, int pageSize, String query) {
        List<Strategy> list = new ArrayList<Strategy>();
        PageHelper.startPage(currentPage, pageSize);
        if (Tools.notEmpty(query)) {
            list = strategyDao.findListByQuery("%" + query + "%");
        } else {
            list = strategyDao.findList();
        }
        PageInfo<Strategy> pageInfo=new PageInfo<Strategy>(list);
        return pageInfo.getList();
    }

    @Override
    public List<Strategy> findByPage(int currentPage, int pageSize) {
        List<Strategy> list = new ArrayList<Strategy>();
        PageHelper.startPage(currentPage, pageSize);
        list = strategyDao.indexList();
        PageInfo<Strategy> pageInfo=new PageInfo<Strategy>(list);
        return pageInfo.getList();
    }

    @Override
    public long state0count() throws Exception {
        return strategyDao.state0count();
    }

    @Override
    public long state1count() throws Exception {
        return strategyDao.state1count();
    }

    @Override
    public long state2count() throws Exception {
        return strategyDao.state2count();
    }
}
